<?php
    class Cms_pro extends CI_Model{

        public function construct()
        {
            parent::__construct();
        }

        /**
        * 
        * return all cms words
        */
        public function get_words()
        {
            return $this->db->get('words')->result_array();
        }
        
        /**
        * 
        * @param $post_ID
        * return cms post
        */
        public function get_post($post_ID)
        {
            return $this->db->get_where('posts', array('ID'=>$post_ID))->row_array();
        }
        
        /**
        * 
        * return cms post news
        */
        public function get_news()
        {
            $q = "
            SELECT posts.ID as post_ID, posts.post_title as post_title , posts.content as content,
            gallery.path as path,
            pictures.filename as filename 
            FROM posts
            LEFT JOIN gallery ON posts.ID = gallery.`posts_ID`
            LEFT JOIN pictures ON gallery.ID = pictures.gallery_ID
            AND pictures.ID=
            (
            SELECT pictures.ID
            FROM pictures
            WHERE pictures.gallery_ID = gallery.ID
            ORDER BY pictures.order ASC, pictures.ID ASC
            LIMIT 0, 1  
            )
            WHERE posts.post_type = 'news'
            ORDER BY posts.ID DESC
            ";
            $res = $this->db->query($q)->result_array();
            return $res;
            
        }
        
        /**
        * 
        * @param $post_ID
        * return gallery ID
        */
        public function get_gallery_id($post_ID)
        {
           $res = $this->db->get_where('gallery', array('posts_ID'=>$post_ID))->row_array();
           if($res){
               return $res['ID'];
           }
           return FALSE;
        }
        
        /**
        * 
        * @param $gallery_ID
        * return cms post
        */
        public function get_gallery($gallery_ID)
        {
            return $this->db->get_where('gallery', array('ID'=>$gallery_ID))->row_array();
        }
        
        /**
        * 
        * @param $gallery_ID
        * return cms post
        */
        public function get_pictures($gallery_ID)
        {
            $this->db->order_by('order asc, ID asc');
            return $this->db->get_where('pictures', array('gallery_ID'=>$gallery_ID))->result_array();
        }

    }
?>
